package 剑指offer;

/**
 * @description:
 * @author: ywk
 * @date: 2021-03-13
 */
public class 寻找缺失重复元素 {
    //注意：0~(n-1)
    static int findErrorNums(int[] nums) {
        int n = nums.length;
        int dup = -1;
        for (int i = 0; i < n; i++) {
            int index = Math.abs(nums[i]);
            if (nums[index] < 0) {
                dup = Math.abs(nums[i]);
            }else{
                nums[index] *= -1;
            }
        }
        int missing = -1;
        for (int i = 0; i < n; i++) {
            if (nums[i] > 0) {
                missing = i;
            }
        }
        return missing;
    }
    //注意：1~n
    static int findErrorNumsN(int[] nums) {
        int n = nums.length;
        int dup = -1;
        for (int i = 0; i < n; i++) {
            int index = Math.abs(nums[i])-1;
            if (nums[index] < 0) {
                dup = Math.abs(nums[i]);
            }else{
                nums[index] *= -1;
            }
        }
        int missing = -1;
        for (int i = 0; i < n; i++) {
            if (nums[i] > 0) {
                missing = i+1;
            }
        }
        return missing;
    }
    public static void main(String[] args) {
        System.out.println(findErrorNums(new int[]{1, 2, 3, 3}));
        System.out.println(findErrorNumsN(new int[]{1, 2, 3, 3}));
    }
}
